package pl.edu.mimuw.irs.rcp.controller.editor.bucket;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.util.ArrayList;
import java.util.Collection;

import pl.edu.mimuw.irs.core.model.analysis.History;

public class Bucket extends ArrayList<History> {

	private static final long serialVersionUID = -7928351726446286936L;

	// *** Properties
	
	public static final String RUN_ADDED = "RUN_ADDED";
	public static final String RUN_REMOVED = "RUN_REMOVED";
	public static final String CONTENT_CHANGED = "CONTENT_CHANGED";
	
	// *** Attributes

	private transient PropertyChangeSupport listeners;
	public PropertyChangeSupport getListeners() {
		if (listeners == null) listeners = new PropertyChangeSupport(this);
		return listeners;
	}
	
	// *** Constructors
	
	public Bucket() {
		
	}
	
	// *** Methods

	@Override
	public boolean add(History e) {
		boolean retval = super.add(e);
		getListeners().firePropertyChange(CONTENT_CHANGED, null, e);
		return retval;
	}
	
	@Override
	public void add(int index, History element) {
		super.add(index, element);
		getListeners().firePropertyChange(CONTENT_CHANGED, null, element);
		return;
	}
	
	@Override
	public History remove(int index) {
		History history = super.remove(index);
		getListeners().firePropertyChange(CONTENT_CHANGED, history, null);
		return history;
	}
	
	@Override
	public boolean remove(Object o) {
		boolean removed = super.remove(o);
		getListeners().firePropertyChange(CONTENT_CHANGED, o, null);
		return removed;
	}
	
	@Override
	public void clear() {
		throw new UnsupportedOperationException();
	}
	
	@Override
	public boolean addAll(Collection<? extends History> c) {
		throw new UnsupportedOperationException();
	}
	
	@Override
	public boolean addAll(int index, Collection<? extends History> c) {
		throw new UnsupportedOperationException();
	}
	
	@Override
	public boolean removeAll(Collection<?> c) {
		throw new UnsupportedOperationException();
	}
	
	@Override
	protected void removeRange(int fromIndex, int toIndex) {
		throw new UnsupportedOperationException();
	}
	
	public void addPropertyChangeListener(PropertyChangeListener listener) {
		getListeners().addPropertyChangeListener(listener);
	}

	public void removePropertyChangeListener(PropertyChangeListener listener) {
		getListeners().removePropertyChangeListener(listener);
	}

	/** Special method for all who wants the listening editors to be notified about the change
	 * @param history history to add
	 */
	public void addRun(History history) {
		add(history);
		getListeners().firePropertyChange(RUN_ADDED, null, history);
	}
}
